//
//  MainViewController.swift
//  54_Weibo(swift)
//
//  Created by 梦然 张 on 16/4/6.
//  Copyright © 2016年 梦然 张. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewControllers()
        
        tabBar.addSubview(composedButton)
//        tabBar.bringSubviewToFront(composedButton)
        
        let count = childViewControllers.count
        let w = tabBar.bounds.size.width / CGFloat(count) + 1
        
//        composedButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        composedButton.frame = CGRect(x: 2*w, y: 0, width: w, height: tabBar.bounds.size.height)
        composedButton.addTarget(self, action: "didClickComposeButton", forControlEvents: .TouchUpInside)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composedButton)
    }
    
    private func addChildViewControllers(){
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(UINavigationController(rootViewController: vc))
    }

    @objc private func didClickComposeButton(){
        print("click compose")
    }
    
}
